package com.zhaohang.cn.mybasicactivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhaohang.cn.activity.VidoListActivity;
import com.zhaohang.cn.constant.ConstantUtils;
import com.zhaohang.cn.fragment.HomeFragment;
import com.zhaohang.cn.fragment.MineFragment;
import com.zhaohang.cn.fragment.ShopingFragment;
import com.zhaohang.cn.fragment.ShowFragment;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends BasicActivity implements View.OnClickListener {


    private static final String HOME_FRAGMENT_KEY      = "HomeFragment";
    private static final String DASHBOARD_FRAGMENT_KEY = "ShopingFragment";
    private static final String CHART_FRAGMENT_KEY     = "ShowFragment";
    private static final String NOTICE_FRAGMENT_KEY    = "MineFragment";
    private FrameLayout mFrameLayout;
    private LinearLayout   mMianHome;
    private ImageView      mMianHomeImage;
    private TextView       mMianHomeText;
    private LinearLayout   mMainShop;
    private ImageView      mMainShopImage;
    private TextView       mMainShopText;
    private LinearLayout   mMianChart;
    private ImageView      mMianChartImage;
    private TextView       mMianChartText;
    private LinearLayout   mMianSelf;
    private ImageView      mMianSelfImage;
    private TextView       mMianSelfText;
    private ImageView      mMainMidleIamge;
    private HomeFragment   mHome;
    private List<Fragment> fragmentList = new ArrayList<>();
    private ShopingFragment mShop;
    private ShowFragment mChart;
    private MineFragment mMain;

    @Override
    protected int setContentSuccessView() {
        return R.layout.activity_main;
    }

    @Override
    public int noNetView() {
        return R.id.footlayout;
    }

    @Override
    protected int setShowState() {
        return ConstantUtils.RELEATSHOW;
    }
    @Override
    protected void getInitView() {
        mFrameLayout= (FrameLayout) findViewById(R.id.mian_fragment);
        
        //首页
        mMianHome=(LinearLayout) findViewById(R.id.mian_home);
        mMianHomeImage= findViewById(R.id.mian_home_iamge);
        mMianHomeText=(TextView) findViewById(R.id.mian_home_text);
        
        //商城
        mMainShop=(LinearLayout) findViewById(R.id.main_shop);
        mMainShopImage=(ImageView) findViewById(R.id.main_shop_iamge);
        mMainShopText=(TextView) findViewById(R.id.main_shop_text);
        
        //购物车
        mMianChart=(LinearLayout) findViewById(R.id.mian_chart);
        mMianChartImage=(ImageView) findViewById(R.id.mian_chart_iamge);
        mMianChartText=(TextView) findViewById(R.id.mian_chart_text);


        //我的
        mMianSelf=(LinearLayout) findViewById(R.id.mian_self);
        mMianSelfImage=(ImageView) findViewById(R.id.mian_self_image);
        mMianSelfText=(TextView) findViewById(R.id.mian_self_text);

        //中间的按钮
        mMainMidleIamge=(ImageView) findViewById(R.id.main_midle_iamge);


        //按钮的点击事件
        mMianHome.setOnClickListener(MainActivity.this);
        mMainShop.setOnClickListener(MainActivity.this);
        mMianChart.setOnClickListener(MainActivity.this);
        mMianSelf.setOnClickListener(MainActivity.this);
        mMainMidleIamge.setOnClickListener(MainActivity.this);

        initButton();

        mMianHomeImage.setSelected(true);
        mMianHomeText.setTextColor(getResources().getColor(R.color.black));


        initFragment();
    }

    private void initFragment() {
        if (mHome ==null){
            mHome = new HomeFragment();
        }
        addFragment(mHome);
        showFragment(mHome);

    }

    private void showFragment(Fragment fragment) {
        for (Fragment frag : fragmentList) {

            if (frag != fragment) {
                /*先隐藏其他fragment*/
                getSupportFragmentManager().beginTransaction().hide(frag).commit();
            }
        }
        getSupportFragmentManager().beginTransaction().show(fragment).commit();
    }

    private void addFragment(Fragment fragment) {
        /*判断该fragment是否已经被添加过  如果没有被添加  则添加*/
        if (!fragment.isAdded()) {
            getSupportFragmentManager().beginTransaction().add(R.id.mian_fragment, fragment).commit();
            /*添加到 fragmentList*/
            fragmentList.add(fragment);
        }
    }


    @Override
    public void onClick(View v) {
        initButton();
        switch (v.getId()){
            //点击首页
            case R.id.mian_home :
                mMianHomeImage.setSelected(true);
                mMianHomeText.setTextColor(getResources().getColor(R.color.black));
                if (mHome == null) {
                    mHome = new HomeFragment();
                }
                addFragment(mHome);
                showFragment(mHome);
                break;
                //商城
            case R.id.main_shop:
                mMainShopImage.setSelected(true);
                mMainShopText.setTextColor(getResources().getColor(R.color.black));
                if (mShop == null) {
                    mShop = new ShopingFragment();
                }
                addFragment(mShop);
                showFragment(mShop);
                break;
                //购物车
            case R.id.mian_chart:
                mMianChartImage.setSelected(true);
                mMianChartText.setTextColor(getResources().getColor(R.color.black));
                if (mChart == null) {
                    mChart = new ShowFragment();
                }
                addFragment(mChart);
                showFragment(mChart);
                break;
                //个人中心
            case R.id.mian_self:
                mMianSelfImage.setSelected(true);
                mMianSelfText.setTextColor(getResources().getColor(R.color.black));
                if (mMain == null) {
                    mMain = new MineFragment();
                }
                addFragment(mMain);
                showFragment(mMain);
                break;
                //中间的大按钮
            case R.id.main_midle_iamge:

                skipOtherActivity(VidoListActivity.class,false);
                break;
        }
    }

    private void initButton() {
        mMianHomeImage.setSelected(false);
        mMainShopImage.setSelected(false);
        mMianChartImage.setSelected(false);
        mMianSelfImage.setSelected(false);

        mMianHomeText.setTextColor(getResources().getColor(R.color.textunselect));
        mMainShopText.setTextColor(getResources().getColor(R.color.textunselect));
        mMianChartText.setTextColor(getResources().getColor(R.color.textunselect));
        mMianSelfText.setTextColor(getResources().getColor(R.color.textunselect));
    }

    //防止fragment重叠
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if (mHome != null) {
            getSupportFragmentManager().putFragment(outState, HOME_FRAGMENT_KEY, mHome);
        }
        if (mShop != null) {
            getSupportFragmentManager().putFragment(outState, DASHBOARD_FRAGMENT_KEY, mShop);
        }
        if (mChart != null) {
            getSupportFragmentManager().putFragment(outState, CHART_FRAGMENT_KEY, mChart);
        }
        if (mMain != null) {
            getSupportFragmentManager().putFragment(outState, NOTICE_FRAGMENT_KEY, mMain);
        }
        super.onSaveInstanceState(outState);
    }
}
